package cn.kgc.ssmmodulardemo.controller;

import cn.kgc.ssmmodulardemo.entity.User;
import cn.kgc.ssmmodulardemo.entity.vo.AddUserVO;
import cn.kgc.ssmmodulardemo.service.UserService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;


/**
 * 用户的控制层
 *
 * @author 27498
 */
@Api(description = "用户管理")
@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @ApiOperation(value = "用户登入", produces = "application/json",
            notes = "状态码：10000:登入成功,10001:用户名或密码的不正确")
    @RequestMapping(value = "/login", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Object userLogin(@RequestParam("userCode") String userCode,
                            @RequestParam("userPassword") String userPassword,
                            HttpSession httpSession) {
        JSONObject jsonObject = new JSONObject();
        //要传输的数据
        String data = "";
        //要传输的信息
        String message = "";
        //状态码！（可以自己定）
        String statCode = "";
        User user = new User();
        user.setUserCode(userCode);
        user.setUserPassword(userPassword);
        User result = userService.userLogin(user);
        if (result != null) {
            data = result.toString();
            message = "登入成功";
            statCode = "10000";
        } else {
            data = "";
            message = "用户名或密码错误";
            statCode = "10001";
        }
        jsonObject.put("data", data);
        jsonObject.put("message", message);
        jsonObject.put("statCode", statCode);
        return jsonObject.toString();
    }


    /**
     * Valid 用来帮我们校验数据的合法性
     *
     * @param addUserVO 视图对象， 要求该对象的属性内容必须要和前端传过来的值一模一样
     * @param result    用来返回对象是否合法
     * @return 返回视图模型 如果合法返回到success 页面 否则 跳到error页面
     */
    @ApiOperation(value = "用户添加",produces = "application/json")
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView testAdd(@RequestBody AddUserVO addUserVO, BindingResult result) {
        ModelAndView modelAndView = new ModelAndView();
        if (result.hasErrors()) {
            System.out.println("监测到数据的不合法性...错误信息如下：");
            System.out.println(result.getFieldErrors().toString());
            modelAndView.setViewName("error");
        } else {
            modelAndView.setViewName("success");
        }
        return modelAndView;
    }

    @ApiOperation(value = "swagger测试",
            produces = "application/json",response = Object.class,httpMethod = "post")
    @RequestMapping(value = "swaggerTest",method = RequestMethod.POST,
            produces = "application/json")
    @ResponseBody
    public Object swaggerTest(@RequestBody AddUserVO addUserVO){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("message","访问成功");
        jsonObject.put("data",addUserVO.toString());
        return jsonObject.toString();
    }


}
